﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace QuMianChaZhi.Contour
{
    public class Triangle
    {
        private Edge a, b, c;

        /// <summary>
        /// 邻边
        /// </summary>
        public Edge A
        {
            get
            {
                return this.a;
            }
            set
            {
                this.a = value;
                if (this.a.Triangle1 == null)
                {
                    this.a.Triangle1 = this;
                }
                else
                {
                    this.a.Triangle2 = this;
                }
            }
        }

        /// <summary>
        /// 对边
        /// </summary>
        public Edge B
        {
            get
            {
                return this.b;
            }
            set
            {
                this.b = value;
                if (this.b.Triangle1 == null)
                {
                    this.b.Triangle1 = this;
                }
                else
                {
                    this.b.Triangle2 = this;
                }
            }
        }

        /// <summary>
        /// 斜边
        /// </summary>
        public Edge C
        {
            get
            {
                return this.c;
            }
            set
            {
                this.c = value;
                if (this.c.Triangle1 == null)
                {
                    this.c.Triangle1 = this;
                }
                else
                {
                    this.c.Triangle2 = this;
                }
            }
        }

        /// <summary>
        /// 是否使用,-1：临时使用; 0:未使用; 1:使用
        /// </summary>
        public int UsedStatus { get; set; }

        public Edge BorderEdge
        {
            get
            {
                if (this.A.IsBorder) { return this.A; }
                if (this.B.IsBorder) { return this.B; }

                return null;
            }
        }

        public Edge[] Get2OtherEdge(Edge relativeEdge)
        {
            Edge[] edges = new Edge[2];
            if (relativeEdge == this.A)
            {
                edges[0] = this.B;
                edges[1] = this.C;
            }
            else if (relativeEdge == this.B)
            {
                edges[0] = this.A;
                edges[1] = this.C;
            }
            else
            {
                edges[0] = this.A;
                edges[1] = this.B;
            }
            return edges;
        }
        
        public Edge[] GetEdges()
        {
            Edge[] edges = new Edge[3];
            edges[0] = this.A;
            edges[1] = this.B;
            edges[2] = this.C;

            return edges;
        }

        public VPoint FindPoint(float x, float y)
        {
            if (this.A != null)
            {
                if (this.A.P1.X == x && this.A.P1.Y == y) { return this.A.P1; }
                if (this.A.P2.X == x && this.A.P2.Y == y) { return this.A.P2; }
            }
            if (this.B != null)
            {
                if (this.B.P1.X == x && this.B.P1.Y == y) { return this.B.P1; }
                if (this.B.P2.X == x && this.B.P2.Y == y) { return this.B.P2; }
            }
            if (this.C != null)
            {
                if (this.C.P1.X == x && this.C.P1.Y == y) { return this.C.P1; }
                if (this.C.P2.X == x && this.C.P2.Y == y) { return this.C.P2; }
            }
            return null;
        }

        public Edge FindEdge(VPoint p1, VPoint p2)
        {
            if (this.A != null)
            {
                if (this.A.P1 == p1 && this.A.P2 == p2) { return this.A; }
            }
            if (this.B != null)
            {
                if (this.B.P1 == p1 && this.B.P2 == p2) { return this.B; }
            }
            if (this.C != null)
            {
                if (this.C.P1 == p1 && this.C.P2 == p2) { return this.C; }
            }
            return null;
        }
    }
}
